package sz.szsmk.citizencard.ui.activity;

import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;

import com.chenenyu.router.Router;
import com.chenenyu.router.annotation.Route;

import sz.szsmk.citizencard.DaggerApplication;
import sz.szsmk.citizencard.R;
import sz.szsmk.citizencard.databinding.SetpsdActivityBinding;
import sz.szsmk.citizencard.dialog.SuccessDialog;
import sz.szsmk.citizencard.ioc.Compontent.DaggerCommonActivityComponent;
import sz.szsmk.citizencard.model.network.bean.response.BaseBean;
import sz.szsmk.citizencard.model.network.bean.response.IbaseBean;
import sz.szsmk.citizencard.model.network.bean.response.UserBean;
import sz.szsmk.citizencard.mvp.contract.ISetPsdContact;
import sz.szsmk.citizencard.mvp.presenter.SetPsdPresenter;
import sz.szsmk.citizencard.ui.base.IBaseActivity;
import sz.szsmk.citizencard.util.ACache;
import sz.szsmk.citizencard.util.AndroidUtils;
import sz.szsmk.citizencard.util.PubUtils;
import sz.szsmk.citizencard.util.StringUtils;

/**
 * Copyright (c)2018
 * author: zdh
 * created on: 2018/6/1 19:20
 * description:设置密码/重置密码
 */
@Route("setpsd")
public class SettingPsdActivity extends IBaseActivity<SetPsdPresenter, SetpsdActivityBinding>
        implements ISetPsdContact.ISetPsdView, View.OnClickListener {

    private String type;
    private ImageView back;
    private Button setpsdSure;
    private String mobile;
    private String sms_code;
    private String oldPsd;
    private String sms_type;

    @Override
    protected int attachLayoutRes() {
        return R.layout.setpsd_activity;
    }

    @Override
    protected void initInjector() {
        DaggerCommonActivityComponent.builder().appCompontent(DaggerApplication.getAppComponent())
                .build().inject(this);
    }

    @Override
    protected void initViews() {
        type = getIntent().getStringExtra("type");
        mobile = getIntent().getStringExtra("mobile");
        sms_code = getIntent().getStringExtra("sms_code");
        sms_type = getIntent().getStringExtra("sms_type");

        if (type != null) {
            if (type.equals("reset")) {
                dataBinding.setPsdReset.setVisibility(View.VISIBLE);
                dataBinding.setpsdTv.setText("修改登录密码");
//            dataBinding.psdTitle.setText("修改登录密码");
            } else {
                dataBinding.setPsdReset.setVisibility(View.GONE);

                dataBinding.setpsdTv.setText("设置登录密码");
//            dataBinding.psdTitle.setText("设置登录密码");
            }
        }

        back = dataBinding.setpsdTitle.back;
        setpsdSure = dataBinding.setpsdSure;
        setpsdSure.setOnClickListener(this);
        back.setOnClickListener(this);
    }

    @Override
    protected void updateViews() {

    }

    @Override
    public void setPsdSuccess(IbaseBean response) {
        Router.build("login")
                .go(this);
        finish();
    }

    @Override
    public void setPsdFail(IbaseBean message) {
        Toast.makeText(this, message.getMessage(), Toast.LENGTH_SHORT).show();
    }


    @Override
    public void modifyPsdSuccess() {
        SuccessDialog dialog = new SuccessDialog(this, "修改成功");
        dialog.setOnOkClickListener(new SuccessDialog.OnOkClickListener() {
            @Override
            public void onOkClick() {
                finish();
            }
        });
        dialog.show();

        finish();
    }

    @Override
    public void modifyPsdFail(String message) {
        Toast.makeText(this, message, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void forgetPsdSucc() {
//        Router.build("login")
//                .go(this);
        finish();
    }

    @Override
    public void forgetPsdFail() {

    }

    @Override
    public void thirdAppLoginSucc() {
        Router.build("login")
                .go(this);
        finish();
    }

    @Override
    public void thirdAppLoginFail() {

    }

    @Override
    public void addUserAppAgreementRecordSucc(BaseBean baseBean) {

    }

    @Override
    public void addUserAppAgreementRecordFail() {

    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.back) {
            finish();
        } else if (id == R.id.setpsd_sure) {
            if (type.equals("reset")) {
                oldPsd = dataBinding.setpsdEtOldpsd.getText().toString();
            }
            String setPsdEt = dataBinding.setpsdEtSetpsd.getText().toString();

            if (type.equals("reset")) {
                if (oldPsd.equals(setPsdEt)) {
                    PubUtils.popTipOrWarn(this, "新密码与老密码一致");
                    return;
                }
            }
            String setPsdEtSure = dataBinding.setpsdEtSurepsd.getText().toString();

            boolean letterDigit = StringUtils.isLetterDigit(setPsdEtSure);

            if (setPsdEt == null || setPsdEt.length() == 0 || setPsdEt.isEmpty()) {
                Toast.makeText(this, "请输入密码", Toast.LENGTH_SHORT).show();
                return;
            }


            if (setPsdEtSure == null || setPsdEtSure.length() == 0 || setPsdEtSure.isEmpty()) {
                Toast.makeText(this, "请再次输入密码", Toast.LENGTH_SHORT).show();
                return;
            }
            if (!setPsdEt.equals(setPsdEtSure)) {
                Toast.makeText(this, "两次输入的密码不一致", Toast.LENGTH_SHORT).show();
                return;
            }

            if (letterDigit) {
//                    Toast.makeText(this, "密码必须同时包含数字和字母", Toast.LENGTH_SHORT).show();
            } else {
                Toast.makeText(this, "密码必须同时包含数字和字母", Toast.LENGTH_SHORT).show();
                return;
            }


            String login_type = ACache.get(SettingPsdActivity.this).getAsString("login_type");
            if (login_type != null) {
                int i = Integer.parseInt(login_type);
                String uid = ACache.get(this).getAsString("uid");
                if (uid != null) {
                    mPresenter.thirdAppLogin(i, mobile, setPsdEtSure, uid);
                }
            } else {
                if (type.equals("reset")) {
                    mPresenter.modifyPsd(setPsdEtSure, oldPsd);
                } else if (type.equals("forget")) {
                    mPresenter.forgetPsd(mobile, setPsdEtSure, sms_code, sms_type);
                } else {
                    mPresenter.register(mobile, setPsdEtSure, sms_code);
                }
            }
        }
    }

    @Override
    public void registerSuccess(UserBean response) {
        //注册成功之后把协议传送给后台
        mPresenter.addUserAppAgreementRecord(AndroidUtils.getVersionName(), "android", "zhsz_app_zhuce",response.getAccess_token());

        Router.build("login")
                .go(this);
        finish();
    }

    @Override
    public void registerFail(String msg) {
        PubUtils.popTipOrWarn(this, msg);
    }

}
